package com.yipei.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yipei.business.domain.MlPositionShare;
import com.yipei.business.service.IMlPositionShareService;
import com.yipei.business.service.IMlTradeAccountService;
import com.yipei.business.vo.BuySellVo;
import com.yipei.business.vo.PositionShareVo;
import com.yipei.common.annotation.Log;
import com.yipei.common.core.controller.BaseController;
import com.yipei.common.core.domain.AjaxResult;
import com.yipei.common.core.domain.entity.MlTradeAccount;
import com.yipei.common.core.page.TableDataInfo;
import com.yipei.common.enums.BusinessType;
import com.yipei.common.utils.SecurityUtils;
import com.yipei.common.utils.StringUtils;
import com.yipei.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 持仓股票Controller
 *
 * @author yipei
 * @date 2024-12-21
 */
@RestController
@RequestMapping("/business/share")
@Api(tags = "持仓股票", description = "持仓股票管理")
public class MlPositionShareController extends BaseController {
    @Autowired
    private IMlPositionShareService mlPositionShareService;
    @Autowired
    private IMlTradeAccountService iMlTradeAccountService;
    /**
     * 查询持仓股票列表
     */
    //@PreAuthorize("@ss.hasPermi('business:share:list')")
    @ApiOperation("持仓股票获取数据")
    @GetMapping("/list")
    public TableDataInfo list(MlPositionShare mlPositionShare) {
        startPage();
        List<MlPositionShare> list = mlPositionShareService.selectMlPositionShareList(mlPositionShare);
        return getDataTable(list);
    }

    @ApiOperation("持仓股票获取数据")
    @GetMapping("/optlist")
    public AjaxResult optlist(PositionShareVo positionShareVo) throws IOException {
//        startPage();
//        List<MlPositionShare> list = mlPositionShareService.selectMlPositionShareList(mlPositionShare);
        MlTradeAccount mlTradeAccount = SecurityUtils.getLoginMember().getMlTradeAccount();
        LambdaQueryWrapper<MlTradeAccount> mlTradeAccountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        mlTradeAccountLambdaQueryWrapper.eq(MlTradeAccount::getTdAcc, mlTradeAccount.getTdAcc());
        MlTradeAccount one = iMlTradeAccountService.getOne(mlTradeAccountLambdaQueryWrapper);
        LambdaQueryWrapper<MlPositionShare> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MlPositionShare::getTdAcc, one.getTdAcc());
        List<MlPositionShare> mlPositionShares = mlPositionShareService.list(lambdaQueryWrapper);
        mlPositionShareService.createpositionShareData(positionShareVo,mlPositionShares,one);
        if (Objects.isNull(positionShareVo)){
            return AjaxResult.error("此账号没有持仓股票！");

        }else {
            return AjaxResult.success(positionShareVo);
        }
    }
    /**
     * 导出持仓股票列表
     */
    //@PreAuthorize("@ss.hasPermi('business:share:export')")
    @ApiOperation("持仓股票导出数据")
    @Log(title = "持仓股票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MlPositionShare mlPositionShare) {
        List<MlPositionShare> list = mlPositionShareService.selectMlPositionShareList(mlPositionShare);
        ExcelUtil<MlPositionShare> util = new ExcelUtil<MlPositionShare>(MlPositionShare.class);
        util.exportExcel(response, list, "持仓股票数据");
    }

    /**
     * 获取持仓股票详细信息
     */
    //@PreAuthorize("@ss.hasPermi('business:share:query')")
    @ApiOperation("持仓股票通过ID获取数据")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mlPositionShareService.selectMlPositionShareById(id));
    }

    /**
     * 新增持仓股票
     */
    //@PreAuthorize("@ss.hasPermi('business:share:add')")
    @ApiOperation("持仓股票新增数据")
    @Log(title = "持仓股票", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MlPositionShare mlPositionShare) {
        return toAjax(mlPositionShareService.insertMlPositionShare(mlPositionShare));
    }

    /**
     * 修改持仓股票
     */
    //@PreAuthorize("@ss.hasPermi('business:share:edit')")
    @ApiOperation("持仓股票修改数据")
    @Log(title = "持仓股票", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MlPositionShare mlPositionShare) {
        return toAjax(mlPositionShareService.updateMlPositionShare(mlPositionShare));
    }

    /**
     * 删除持仓股票
     */
    //@PreAuthorize("@ss.hasPermi('business:share:remove')")
    @ApiOperation("持仓股票删除数据")
    @Log(title = "持仓股票", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mlPositionShareService.deleteMlPositionShareByIds(ids));
    }

    @ApiOperation("获取仓位和资金信息/可卖，可买数据")
    @GetMapping("/asset")
    public AjaxResult getPositionShare(@RequestParam("f12") String f12,@RequestParam("price") BigDecimal price, @RequestParam(name="id",required = false) String id) throws Exception {
        if (StringUtils.isEmpty(id)){
         return    AjaxResult.error("");
        }
        BuySellVo buySellVo = mlPositionShareService.ceratePositionShare(f12,price,id);
        return AjaxResult.success(buySellVo);
    }



}
